--require "MainScene"

JLAllLayer={}
JLAllLayerFunc={}
JLAllLayerFunc.__index = JLAllLayerFunc
setmetatable(JLAllLayer, JLAllLayerFunc)

JLAllLayerFunc.getNode = function(gid)
  local l = {}
  setmetatable(l, JLAllLayerFunc)
  l.node = CCBReader:load("JunXiaoLayer3.ccbi")
  print("JLAllLayer.getNode", JLAllLayer.node)
  JLAllLayer.instance = l
  l:init(gid) 
  return l 
end

JLAllLayerFunc.getScene = function(self)
  local sc = CCScene:create()
  sc:addChild(JLAllLayer:getNode().node)  
  print("JLAllLayer.getScene", sc)
  return sc
end

JLAllLayerFunc.init = function(self, gid)
  --self:initTable()
  setObjectExtTable(self.node:getChildByTag(1022), self)
  setObjectExtTable(self.node:getChildByTag(1023), self)
  setObjectExtTable(self.node:getChildByTag(1024), self)
  setObjectExtTable(self.node:getChildByTag(1025), self)
  setObjectExtTable(self.node:getChildByTag(1026), self)
  self.generals= General:getGenerals()
  local bl = TypeCast(self.node:getChildByTag(1020), "CCButton")
  local br = TypeCast(self.node:getChildByTag(1021), "CCButton")
  self.uiStepper = UIStepper:create(bl, br, "onUIStepperChanged", self)
  self.uiStepper:setCount(#self.generals)
  self.uiStepper:setIndex(0)
  --self.currentGeneral = 1
  --self:setHeroLabels(self.currentGeneral)
  --self:setEquipView()
end
JLAllLayerFunc.setHeroLabels = function(self, gid)
  local h = HeroData:create(gid)
  if self.headSp~=nil then
    self.headSp:getParent():removeChild(self.headSp, true)
    self.headSp=nil
  end
  local imgName = string.format("%s_100.png", h.heroConfig.asset)
  if tonumber(gid)==1 then
      imgName = nameFromPath(UserData.userinfo.host.headpic)
  end
  self.headSp = addSpriteFromBK(self.node, 1030, imgName)
  TypeCast(self.node:getChildByTag(1009), "CCLabelTTF"):setString(h.heroConfig.name)
  TypeCast(self.node:getChildByTag(1009), "CCLabelTTF"):setColor(Equip.EquipColor[h.heroConfig.quality])
 
  TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(string.format("Lv%d", h.heroInfo.l))

  local needXP = UserData.config.models.RankLevel[h.heroInfo.l].threshold
  local curXP = h.heroInfo.x --UserData.userinfo.host.xp
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d/%d", curXP, needXP))
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%d", h.heroConfig.initForces))
  TypeCast(self.node:getChildByTag(1013), "CCLabelTTF"):setString(string.format("%d", h.heroConfig.initCommander))
  TypeCast(self.node:getChildByTag(1014), "CCLabelTTF"):setString(string.format("%d", h.heroConfig.initTactics))
  TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setString(string.format("%d", h.heroConfig.initBlast))

  local skills = UserData.config.models.NewSkill
  TypeCast(self.node:getChildByTag(1017), "CCLabelTTF"):setString(h.heroConfig.desc)
  TypeCast(self.node:getChildByTag(1018), "CCLabelTTF"):setString(skills[h.heroConfig.skill].name)
  TypeCast(self.node:getChildByTag(1019), "CCLabelTTF"):setString(autoEnter(skills[h.heroConfig.skill].desc, 36))
end
JLAllLayerFunc.setEquipView = function(self)
  if self.equipButtons~=nil then
    for k,v in pairs(self.equipButtons) do
      if v~=nil then
        self.node:getChildByTag(1001):removeChild(v,true)
      end
    end
  end
  self.equipButtons = {} 
  local equipIDs = UserData.userinfo.host.equips
  local equipIndexs = UserData.userinfo.host.armyGroups[tostring(self.currentGeneral)].e
  for k,v in ipairs(equipIndexs) do
    if v>0 then
      local eid = equipIDs[tostring(v)].e
      local einfo = UserData.config.models.Equip[eid]
      local imgName = nameFromPath(einfo.asset)
      local bkImg = self.node:getChildByTag(1001):getChildByTag(k)
      local button = CCButton:createWithFileName("onClickEquip", imgName, imgName, imgName, "")
      setObjectExtTable(button, self)
      button:setTag(v)
      button:setPosition(bkImg:getPosition())
      self.node:getChildByTag(1001):addChild(button)
      self.equipButtons[#self.equipButtons+1] = button
    end
  end 
end
JLAllLayerFunc.onUIStepperChanged = function(self, index)
  self.currentGeneral = self.generals[index+1].id
  self:setHeroLabels(self.currentGeneral)
  self:setEquipView()
end
JLAllLayerFunc.onClickEquip = function(self, node, event)
end
JLAllLayerFunc.onHuanZhuangButton = function(self, node, event)
  JLAllLayer.oldIndex = self.uiStepper.index
  local l = ZhuangBeiScene.getNode()
  l.toNode = self 
  l.callback = JLAllLayerFunc.onReturn2Node
  l.contentLayer.heroEquipLayer:setTableFocusById(self.currentGeneral)
  MainScene.scene:addChild(l.node)
  --setTouchEnabledAll(self.node, false)
  MainScene.scene:removeChild(JunXiaoScene.instance.node, true)
end
JLAllLayerFunc.onXunZhangButton = function(self, node, event)
  JLAllLayer.oldIndex = self.uiStepper.index
  local l = ShouXunScene1.getNode()
  l.toNode = self 
  l.callback = JLAllLayerFunc.onReturn2Node 
  MainScene.scene:addChild(l.node)
  --setTouchEnabledAll(self.node, false)
  MainScene.scene:removeChild(JunXiaoScene.instance.node, true)
end
JLAllLayerFunc.onUpgradeButton = function(self, node, event)
  JLAllLayer.oldIndex = self.uiStepper.index
  MainScene.scene:removeChild(JunXiaoScene.instance.node, true)
  MainScene.scene:addChild(JLSJLayer:getNode(self.currentGeneral).node)
end
JLAllLayerFunc.onPeiYangButton = function(self, node, event)
  JLAllLayer.oldIndex = self.uiStepper.index
  MainScene.scene:removeChild(JunXiaoScene.instance.node, true)
  MainScene.scene:addChild(JLPYLayer:getNode(self.currentGeneral).node)
end
JLAllLayerFunc.onQiangHuaButton = function(self, node, event)
  JLAllLayer.oldIndex = self.uiStepper.index
  local l = ZhuangBeiScene.getNode()
  l.toNode = self 
  l.callback = JLAllLayerFunc.onReturn2Node 
  l.radioMenu:setMenuState(1)
  l.contentLayer.contentLayer:setTableFocusById(self.currentGeneral)
  MainScene.scene:addChild(l.node)
  --setTouchEnabledAll(self.node, false)
  MainScene.scene:removeChild(JunXiaoScene.instance.node, true)
end
JLAllLayerFunc.onReturn2Node = function(self)
  print("JLAllLayerFunc.onReturn2Node")
  local l = JunXiaoScene:getNode()
  l.radioMenu:setMenuState(3)
  print("JLAllLayerFunc.onCloseButton", JLAllLayer.oldIndex)
  JLAllLayer.instance.uiStepper:setIndex(JLAllLayer.oldIndex)
  MainScene.scene:addChild(l.node)
end

